package cn.edu.xmu.javaee.chapter2.beanlifecycle.bean;

import cn.edu.xmu.javaee.chapter2.beanlifecycle.config.ApplicationConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

/**
 * @author: Ming Qiu
 * @date: Created in 21:07 2020/7/31
 **/
@Component
@Slf4j
@RequiredArgsConstructor
public class MyBeanPostProcessor implements BeanPostProcessor {

    private final MessageSource messageSource;

    private final ApplicationConfig config;

    //实例化完成，setBeanName/setBeanFactory完成之后调用该方法
    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        log.debug(messageSource.getMessage("postProcessBeforeInitialization",new Object[] {"MyBeanPostProcessor",s}, "property postProcessBeforeInitialization not exist", config.getLocale()));
        return o;
    }

    //全部是实例化完成以后调用该方法
    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        log.debug(messageSource.getMessage("postProcessAfterInitialization",new Object[] {"MyBeanPostProcessor",s}, "property postProcessBeforeInitialization not exist", config.getLocale()));
        return o;
    }

}
